﻿using System;
using System.Reflection;

namespace UtilZ.Dotnet.Ex.Json
{
    /// <summary>
    /// object json转换器
    /// </summary>
    public class ObjectJsonValueConverter : JsonValueConverterAbs
    {
        private readonly static ObjectJsonValueConverter _default;
        /// <summary>
        /// 默认实例
        /// </summary>
        public static ObjectJsonValueConverter Default
        {
            get { return _default; }
        }

        static ObjectJsonValueConverter()
        {
            _default = new ObjectJsonValueConverter();
        }





        /// <summary>
        /// 构造函数
        /// </summary>
        public ObjectJsonValueConverter()
            : base(true)
        {

        }








        /// <summary>
        /// 尝试将对象值转换为json值[转换成功返回true,失败返回false]
        /// </summary>
        /// <param name="memberInfo">成员信息</param>
        /// <param name="valueType">值类型</param>
        /// <param name="owner">成员所属对象</param>
        /// <param name="value">属性或字段值</param>
        /// <param name="jsonValue">成员值</param>
        /// <returns>转换成功返回true,失败返回false</returns>
        protected override bool PrimitiveTryConvertTo(MemberInfo memberInfo, Type valueType, object owner, object value, out string jsonValue)
        {
            bool result = true;

            if (value == null)
            {
                jsonValue = null;
            }
            else
            {
                valueType = value.GetType();

                IJsonValueConverter converter;
                if (JsonConverterManger.TryGetDefaultConverter(valueType, out converter))
                {
                    if (converter == this)
                    {
                        jsonValue = value.ToString();
                    }
                    else
                    {
                        result = converter.TryConvertTo(memberInfo, valueType, owner, value, out jsonValue);
                    }
                }
                else
                {
                    jsonValue = value.ToString();
                }
            }

            return result;
        }





        /// <summary>
        /// 尝试将json值转换为对象值[转换成功返回true,失败返回false]
        /// </summary>
        /// <param name="memberInfo">成员信息</param>
        /// <param name="valueType">值类型</param>
        /// <param name="owner">成员所属对象</param>
        /// <param name="jsonValue">json值</param>
        /// <param name="value">对象值</param>
        /// <returns>转换成功返回true,失败返回false</returns>
        protected override bool PrimitiveTryConvertFrom(MemberInfo memberInfo, Type valueType, object owner, string jsonValue, out object value)
        {
            bool result = true;
            if (string.IsNullOrEmpty(jsonValue) || JsonHelper.IsNull(jsonValue))
            {
                value = null;
                result = false;
            }
            else
            {
                if (long.TryParse(jsonValue, out var l))
                {
                    value = l;
                }
                else if (double.TryParse(jsonValue, out var d))
                {
                    value = d;
                }
                else if (DateTime.TryParse(jsonValue, out var dt))
                {
                    value = dt;
                }
                else if (bool.TryParse(jsonValue, out var b))
                {
                    value = b;
                }
                else
                {
                    value = jsonValue;
                }
            }

            return result;
        }
    }
}
